package com.aliengen.thingple.driverapp.device;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.aliengen.thingple.driverapp.R;
import com.handset.Device;

import java.util.Timer;
import java.util.TimerTask;

/**
 * This class is part of the Thingple mobile application for the CMC168 Handheld reader.
 *
 * Copyright AlienGen Limited 2016. All rights reserved.
 *
 * @author Timothé Mermet-Buffet <tim@aliengen.com>
 */
public class CMCHandler extends Handler {
    Activity mActivity = null;

    private OnEventListener mOnEventListener;

    //add
    public final int CMCID_RETURN_SUCCESS = 0;
    public final int CMCID_RETURN_FAILED = 0x80;
    /*Key Type*/
    public final byte PASSWORD_TYPE_A = 0x60;
    public final byte PASSWORD_TYPE_B = 0x61;

    // Message Type
    public final static int RET_RECEIVE_DATA = 1;
    public final static int RET_OPERATE = 0x1C;
    public final static int CMCID_ERROR = -1;

    /*Error code*/
    public final int CMCID_ERROR_CMDORLENGTH = 0x81;
    public final int CMCID_ERROR_BCC = 0x82;
    public final int CMCID_ERROR_NOCARD = 0x83;
    public final int CMCID_ERROR_SN = 0x84;
    public final int CMCID_ERROR_PWD = 0x85;
    public final int CMCID_ERROR_OPERATE = 0x86;

    public final static int READ_TIMEOUT = 110;
    public final static int WRITE_TIMEOUT = 111;
    public final static int SELECT_TIMEOUT = 112;

    Timer timer;
    TimerTask timerTask;

    public interface OnEventListener {
        void onPressDown();

        void onPressUp();

        void onReceiveData();

        void onRead(String data);

        void onReadFailed();

        void onWrite();

        void onWriteFailed();

        void onTagSelected();

        void onTagSelectFailed();
    }

    public void setActivity(Activity activity)
    {
        Log.i("CMCHandler", "setActivity: " + activity.getLocalClassName());
        this.mActivity = activity;
    }

    public void setOnEventListener(OnEventListener listener) {
        Log.i("CMCHandler", "setOnEventListener");
        mOnEventListener = listener;
    }

    public void setTimer(final int signal, int milliseconds)
    {
        timer = new Timer(true);
        timerTask = new TimerTask() {
            public void run() {
                CMCHandler.this.obtainMessage(signal).sendToTarget();
            }
        };
        timer.schedule(timerTask, milliseconds);
    }

    public void handleMessage(Message msg) {
        switch (msg.what) {
            case Device.CMCID_MSG_BATTERY:
                String strBty = msg.obj.toString();
                showToast("MSG_BATTERY: " + strBty);
                Log.i("CMCHandler", "MSG_BATTERY: " + strBty);
                break;
            case RET_RECEIVE_DATA:
                Log.i("CMCHandler", "RET_RECEIVE_DATA");
                if (mOnEventListener != null)
                    mOnEventListener.onReceiveData();
                break;
            case Device.CMCID_MSG_FWVERSION:
                String strVersion = msg.obj.toString();
                Log.i("CMCHandler", "CMCID_MSG_FWVERSION " + strVersion);
                break;
            case Device.CMCID_MSG_UPDATEBARCODE:
                Log.i("CMCHandler", "CMCID_MSG_UPDATEBARCODE");
                break;
            case Device.CMCID_MSG_BARCODE:
                Log.i("CMCHandler", "CMCID_MSG_BARCODE");
                break;
            case Device.CMCID_MSG_SETPOWER:
                if (msg.arg1 == 0) {
                    Log.i("CMCHandler", "CMCID_MSG_SETPOWER Success!");
                } else {
                    Log.e("CMCHandler", "CMCID_MSG_SETPOWER Failed!");
                }
                break;
            case Device.CMCID_MSG_READ:
                Log.i("CMCHandler", "CMCID_MSG_READ");
                timerTask.cancel();
                timer.cancel();
                String strTag = msg.obj.toString().trim();
                if (strTag.equals("80")) {
                    if (mOnEventListener != null) {
                        mOnEventListener.onReadFailed();
                    }
                }
                else {
                    if (mOnEventListener != null) {
                        mOnEventListener.onRead(strTag);
                    }
                }
                break;
            case READ_TIMEOUT:
                Log.i("CMCHandler", "READ_TIMEOUT");
                timerTask.cancel();
                timer.cancel();
                if (mOnEventListener != null) {
                    mOnEventListener.onReadFailed();
                }
                break;
            case WRITE_TIMEOUT:
                Log.i("CMCHandler", "WRITE_TIMEOUT");
                timerTask.cancel();
                timer.cancel();
                if (mOnEventListener != null) {
                    mOnEventListener.onWriteFailed();
                }
                break;
            case SELECT_TIMEOUT:
                Log.i("CMCHandler", "SELECT_TIMEOUT");
                timerTask.cancel();
                timer.cancel();
                if (mOnEventListener != null) {
                    mOnEventListener.onTagSelectFailed();
                }
                break;
            case Device.CMCID_MSG_WRITE:
                Log.i("CMCHandler", "CMCID_MSG_WRITE");
                timerTask.cancel();
                timer.cancel();
                if (msg.arg1 == 0) {
                    Log.i("CMCHandler", "CMCID_MSG_WRITE Success!");
                    if (mOnEventListener != null) {
                        mOnEventListener.onWrite();
                    }
                } else {
                    Log.e("CMCHandler", "CMCID_MSG_WRITE Failed!");
                    if (mOnEventListener != null) {
                        mOnEventListener.onWriteFailed();
                    }
                }
                break;
            case Device.CMCID_MSG_FINGER_BITMAP:
                Log.i("CMCHandler", "CMCID_MSG_FINGER_BITMAP");
                break;
            case CMCID_RETURN_FAILED:
                Log.i("CMCHandler", "CMCID_RETURN_FAILED Read serial number failed!");
                break;
            case CMCID_ERROR_CMDORLENGTH:
                Log.i("CMCHandler", "CMCID_ERROR_CMDORLENGTH Command Code Error.");
                break;
            case CMCID_ERROR_BCC:
                Log.i("CMCHandler", "CMCID_ERROR_BCC");
                break;
            case CMCID_ERROR_NOCARD:
                Log.i("CMCHandler", "CMCID_ERROR_NOCARD");
                break;
            case CMCID_ERROR_SN:
                Log.i("CMCHandler", "CMCID_ERROR_SN");
                showToast("SN Error");
                break;
            case CMCID_ERROR_PWD:
                Log.i("CMCHandler", "CMCID_ERROR_PWD");
                break;
            case CMCID_ERROR_OPERATE:
                Log.i("CMCHandler", "CMCID_ERROR_OPERATE");
                break;
            case CMCID_ERROR:
                Log.i("CMCHandler", "CMCID_ERROR");
                break;
            case RET_OPERATE:
                Log.i("CMCHandler", "RET_OPERATE");
                break;
            case 100:
                String mTestMessage = msg.obj.toString();
                System.out.println("CMCHandler: [100] " + mTestMessage);
                Log.i("CMCHandler", "[100] " + mTestMessage);
                break;
            case Device.CMCID_MSG_PRESS_DOWN:
                Log.i("CMCHandler", "CMCID_MSG_PRESS_DOWN");
                if (mOnEventListener != null)
                    mOnEventListener.onPressDown();
                break;
            case Device.CMCID_MSG_PRESS_UP:
                Log.i("CMCHandler", "CMCID_MSG_PRESS_UP");
                if (mOnEventListener != null)
                    mOnEventListener.onPressUp();
                break;
            case Device.CMCID_MSG_SET_SELECT:
                Log.i("CMCHandler", "CMCID_MSG_SET_SELECT");
                timerTask.cancel();
                timer.cancel();
                if (mOnEventListener != null) {
                    mOnEventListener.onTagSelected();
                }
                break;
            case Device.CMCID_MSG_EXIT_SELECT:
                Log.i("CMCHandler", "CMCID_MSG_EXIT_SELECT");
                break;
            default:
                Log.i("CMCHandler", "Unknown code " + msg.what);
                break;
        }
    }

    public void showToast(final CharSequence message) {
        if (mActivity == null) {
            return;
        }

        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(mActivity.getApplicationContext(), message, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void showToast(final int message) {
        if (mActivity == null) {
            return;
        }

        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(mActivity.getApplicationContext(), message, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
